#define _CRT_SECURE_NO_WARNINGS


//https://www.luogu.com.cn/problem/P1119


#include<iostream>
#include<cstring>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
int n, m;
int t[N];
int dp[N][N];
void floyd(int k)
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> t[i];
	memset(dp, 0x3f,sizeof dp);
	for (int i = 0; i < n; i++)
		dp[i][i] = 0;
	for(int i=1;i<=m;i++)
	{
		int u, v, w;
		cin >> u >> v >> w;
		dp[u][v] = dp[v][u] = min(dp[u][v], w);
	}

	int q;
	cin >> q;
	int pos = 0;
	while (q--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		while (pos < n && t[pos] <= c)
			floyd(pos++);

		if (t[a] > c || t[b] > c || dp[a][b] == INF)
			cout << "-1\n";
		else
			cout << dp[a][b] << "\n";
	}
	return 0;
}



